package cn.c3n.demo.handle;

import cn.c3n.demo.domain.Result;
import cn.c3n.demo.exception.GirException;
import cn.c3n.demo.utils.ResultUtil;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class ExceptionHandle {

    @ResponseBody //由于要返回的是JSON串，又没有RestController
    @ExceptionHandler(value = Exception.class)//需要捕获的类
    public Result handle(Exception e)
    {
        if(e instanceof GirException)
        {
            GirException ee = (GirException)e;
            return ResultUtil.error(ee.getCode(), e.getMessage());
        }
        else
        {
            return ResultUtil.error(-1, "未知错误");
        }

    }
}
